using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace tKitchen
{
    public partial class MealCalendar : UserControl
    {
        DateTime currentDate;
        Dictionary<int, MealCalendarDay> dayList;
        private const int NUM_OF_DAY_BOXES = 42;
        string monthYear;
        List<MealPlan> data;
        

        #region Properties
        public string MonthYear
        {
            get { return monthYear; }
            set { monthYear = value; }
        }
        public DateTime Date
        {
            get { return currentDate; }
            set
            {
                currentDate = value;
                DisplayCalendar();
            }
        }
 /*       public List<MealPlan> Data
        {
            get { return data; }
            set { data = value; }
       }
   */
        public List<MealPlan> GetDatasource()
        {
            return data;
        }
        public void SetDatasource(List<MealPlan> value)
        {
            data = value;
        }
        #endregion


        public event EventHandler MealDoubleClicked = delegate { };
        public event EventHandler NewMealMenuItemClicked = delegate { };
        public event EventHandler DeleteMenuItemClicked = delegate { };


        public MealCalendar()
        {
            InitializeComponent();

            dayList = new Dictionary<int, MealCalendarDay>(NUM_OF_DAY_BOXES);
            currentDate = DateTime.Today;
            data = new List<MealPlan>();
            

            //add all the day boxes to the list
            foreach (object control in this.Controls)
            {

                if (control.GetType() == typeof(MealCalendarDay))
                {
                    MealCalendarDay tempDay = (MealCalendarDay)control;

                    //each MealCalendarDay control has the name d##, where ## is the index in the
                    //month grid, so we use this as its key in the dictionary
                    int tempNum = Int32.Parse(tempDay.Name.Substring(1));
                    tempDay.ListDoubleClicked += new EventHandler(MealCalendar_ListDoubleClicked);
                    tempDay.DeleteMenuItemClicked += new EventHandler(tempDay_DeleteMenuItemClicked);
                    tempDay.NewMealMenuItemClicked += new EventHandler(tempDay_NewMealMenuItemClicked);

                    dayList.Add(tempNum, tempDay);
                }
            }


            DisplayCalendar();



        }




        public void DisplayCalendar()
        {
            //change the month/year label
            monthYear = currentDate.ToString("MMMM") + " " + currentDate.Year;

            //set the day number for each of the day boxes

            //subtract off days to get to the start of the month
            DateTime monthStart = currentDate.AddDays((-currentDate.Day) + 1);
            int firstDayOfMonth = (int)monthStart.DayOfWeek;

            //add a full month, then go back one day to puts you at the last day of the month.
            int numDaysInMonth = monthStart.AddMonths(1).AddDays(-1).Day;

            for (int i = 0; i < dayList.Count; i++)
            {

                int dayNum = i - firstDayOfMonth +1;
                if (dayNum > 0 && dayNum <= numDaysInMonth)
                {
                    dayList[i].DayNumber = dayNum;
                    dayList[i].Enabled = true;
                    dayList[i].List.DisplayMember = "Name";
                   }
                else
                {
                    dayList[i].DayNumber = -1;
                    dayList[i].Enabled = false;

                }

                dayList[i].List.Items.Clear();

            }

            for (int i = 0; i < data.Count; i++)
            {
                DateTime mealDate = data[i].Date;
                int mealDay = mealDate.Day;
                int mealMonth = mealDate.Month;
                int mealYear = mealDate.Year;
                if (mealMonth == Date.Month && mealYear == Date.Year)
                {
                    int index = mealDay + firstDayOfMonth -1;
                    dayList[index].List.Items.Add(data[i]);
                }

            }


        }

        void tempDay_NewMealMenuItemClicked(object sender, EventArgs e)
        {
            MealCalendarDay day = (MealCalendarDay)sender;

            DateTime clickedDate = this.Date;
            double diff = clickedDate.Day - this.Date.Day;
            clickedDate.AddDays(diff);

            NewMealMenuItemClicked(clickedDate, e);
        }

        void tempDay_DeleteMenuItemClicked(object sender, EventArgs e)
        {
            //if there is an item selected, fire the event
            ListBox temp = (ListBox)sender;
            if (temp.SelectedIndex > -1)
            {
                DeleteMenuItemClicked(sender, e);
            }
        }


        void MealCalendar_ListDoubleClicked(object sender, EventArgs e)
        {
            ListBox temp = (ListBox)sender;
            if (temp.SelectedIndex > -1)
            {
                MealDoubleClicked(sender, e);
            }
        
        }

        private void d1_Load(object sender, EventArgs e)
        {

        }


    }
}
